package com.MarkToTop.main;

import com.MarkToTop.util.Constant;
import com.MarkToTop.util.GameUtil;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * MarkWang 版权所有 © Copyright 2020
 *
 * @CreateDate: Created in 2020-06-21 22:48
 * @Author: MarkToTop -(づ｡◕‿‿◕｡)づ
 * @Description:   对于游戏障碍物的定义
 */
public class Barrier {

    //障碍物需要的图片资源，只需要加载一次
    private static BufferedImage[] BarrImages;
    static {
        //类加载时，初始化图片对象
        BarrImages = new BufferedImage[3];
        for (int i = 0; i < BarrImages.length; i++) {
            BarrImages[i] = GameUtil.loadBufferedImage(Constant.Barriers_IMG_PATH[i]);
        }
    }

    private int x,y;
    private int width,height;

    //障碍物的移动速度
    private int speed;

    //获得所有碰撞元素的宽高
    public static final int BARRIER_WIDTH = BarrImages[0].getWidth();
    public static final int BARRIER_HEIGHT = BarrImages[0].getHeight();
    public static final int BARRIER_HEAD_WIDTH = BarrImages[1].getWidth();
    public static final int BARRIER_HEAD_HEIGHT = BarrImages[1].getHeight();

    //障碍物类型
    private int type;
    public static final int TYPE_TOP_NORMAL    = 0;  //上方，正常
    public static final int TYPE_BOTTOM_NORMAL = 1;  //下方，正常
    public static final int TYPE_TOP_HARD      = 2;  //上方，会伸缩
    public static final int TYPE_BOTTOM_HARD   = 3;  //下方，会伸缩
    public static final int TYPE_HOVER_NORMAL  = 4;  //悬浮，正常
    public static final int TYPE_HOVER_HARD    = 5;  //悬浮，会伸缩

    //添加障碍物可见状态，用于回收   false表示可收回
    private boolean visible;

    //添加障碍物碰撞矩形
    private Rectangle rect;


    public Barrier(){
        this.width = BARRIER_WIDTH;//默认障碍物宽
        this.speed = Constant.BARRIES_MIN_SPEED;  //障碍物初始速度

        rect = new Rectangle();
        rect.width = this.width;
    }

    public void draw(Graphics g,Bird bird){
        switch (type){
            case TYPE_TOP_NORMAL:
                drawTopNormalBarr(g);
                break;
            case TYPE_BOTTOM_NORMAL:
                drawBottomNormalBarr(g);
                break;
            case TYPE_HOVER_NORMAL:
                drawHoverNormalBarr(g);
                break;
        }
        if(bird.isDie()) return;  //鸟落地后障碍物不动
            BarrLogic();
    }

    /**
     * 绘制顶部正常状态的障碍物
     *     1、随机确定障碍物的高度，考虑最小高度
     * @param g
     */
    private void drawTopNormalBarr(Graphics g){
        //确定上半部分拼接的个数
        int count = (height - BARRIER_HEAD_HEIGHT) / BARRIER_HEIGHT;
        for (int i = 0; i < count+1; i++) {
            g.drawImage(BarrImages[0],x,y+ i* BARRIER_HEIGHT,null);
        }
        //绘制障碍物头部分
        int y = this.y + height - BARRIER_HEAD_HEIGHT;
        g.drawImage(BarrImages[2],x-(BARRIER_HEAD_WIDTH - BARRIER_WIDTH >> 1),y,null);
    }

    /**
     * 绘制底部正常状态的障碍物
     * @param g
     */
    private void drawBottomNormalBarr(Graphics g){
        //确定下半部分拼接的个数
        int count = (height - BARRIER_HEAD_HEIGHT) / BARRIER_HEIGHT + 1; //多画一个。跑出屏幕底
        for (int i = 0; i < count; i++) {
            g.drawImage(BarrImages[0],x,y + i* BARRIER_HEIGHT + BARRIER_HEAD_HEIGHT,null);
        }
        //绘制障碍物头部分
        g.drawImage(BarrImages[1],x-(BARRIER_HEAD_WIDTH - BARRIER_WIDTH >> 1),y,null);
    }

    /**
     * 绘制悬浮正常状态的障碍物
     * @param g
     */
    private void drawHoverNormalBarr(Graphics g){
        //确定中间拼接块数
        int count = (height - BARRIER_HEAD_HEIGHT*2) / BARRIER_HEIGHT +1;
        //绘制障碍物上方头部分
        g.drawImage(BarrImages[1],x-(BARRIER_HEAD_WIDTH - BARRIER_WIDTH >> 1),y,null);
        //绘制中间细的部分
        for (int i = 0; i < count; i++) {
            g.drawImage(BarrImages[0],x,y + i* BARRIER_HEIGHT + BARRIER_HEAD_HEIGHT,null);
        }
        //绘制障碍物下方头部分
        int y = this.y + height - BARRIER_HEAD_HEIGHT;
        g.drawImage(BarrImages[2],x-(BARRIER_HEAD_WIDTH - BARRIER_WIDTH >> 1),y,null);

    }

    /**
     * 障碍物的逻辑处理部分
     *    容器中的每个障碍物都携带该逻辑运算
     */
    private void BarrLogic(){
        x -= speed;
        rect.x -=speed;
        //障碍物出屏幕，可回收
        if(x < -BARRIER_WIDTH){
            visible = false;
        }
    }

    /**
     * 判断障碍物是否在屏幕中
     * @return
     */
    public boolean isInFrame(){
        return x +BARRIER_WIDTH < Constant.Frame_Width;
    }

    public int getX(){
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }
    public void setY(int y) {
        this.y = y;
    }
    public void setType(int type) {
        this.type = type;
    }
    public void setHeight(int height) {
        this.height = height;
    }
    public void setVisible(boolean visible) {
        this.visible = visible;
    }
    public boolean isVisible() {
        return visible;
    }
    public Rectangle getRect() {
        return rect;
    }

    /**
     * 设置碰撞障碍物的属性
     */
    public void setAttribute(int x, int y, int height, int type, boolean visible){
        this.x = x;
        this.y = y;
        this.height = height;
        this.type = type;
        this.visible = visible;
        setRectangle(x,y,height); //设置障碍物碰撞矩形
    }

    /**
     *设置障碍物碰撞矩形的属性
     */
    public void setRectangle(int x, int y, int height){
        rect.x =x;
        rect.y = y;
        rect.height = height;
    }
}